package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.downorderrefund.IDownOrderRefundService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/downorderrefund/")
public class DownOrderRefundRest extends AbsRest{
	@Autowired
	IUserService userService;

	@Autowired
	IDownOrderRefundService refundService;

	@SuppressWarnings("static-access")
	@POST
	@Path("findPageOrderRefund")
	public String findPageOrderRefund(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request, userService, "start", "pageSize","dowonphonenum","useruuid","starttime");
		if (result == null) {
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject userInfo = JsonUtils.converToJson(jwt.getSubject());
						if (userInfo != null) {
							JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
							Map<String, Object> parameters = MapUtil.getMap();
							User adminUser = userService.findByUserid(userInfo.getString(Config.tokenSignAttr));
							if (adminUser != null && UserUtil.checkRoleDown(adminUser)) {
									parameters.put(":useruuid", adminUser.getUseruuid());
							}
							if(!StringUtils.isBlank(json.getString("useruuid"))){
								parameters.put(":useruuid", json.getString("useruuid"));
							}
							if(!StringUtils.isBlank(json.getString("dowonphonenum"))){
								parameters.put(":dowonphonenum", json.getString("dowonphonenum"));
							}
							if(!StringUtils.isBlank(json.getString("starttime"))){
								parameters.put(":starttime", json.getString("starttime"));
							}
							if(!StringUtils.isBlank(json.getString("endtime"))){
								parameters.put(":endtime", json.getString("endtime"));
							}
							return ResultMessage.getSucceed( JsonUtils.excludes(this.refundService.findPageDownorderrefund(parameters, json.getInt("start"), json.getInt("pageSize")),
									new String[]{"orderMatchDetails","upOrders","downOrderLogs","downOrderRefunds","discountAmount","downShopProduct","callNum",
											"discount","callStatus","busCode","downreqnum","downendtimesec","downnotificationaddress","downrechargenum","rechargeamount",
											"remark","downendtime","province","statusremark","createtime","status","downreqid","operators","userFlow","flowInformation",
											"downorderuuid","downRechargeGame","phonenum","password","userstatus","usertype","token","wechatid","errorCodesForUseruuid",
											"expire","createtime","lastlogintime"}));
						}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}
}
